// Komissarov Denis
// 18.03.2011

package GUI;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;

public class CLEDItem {

	private int width; // total width
	private int height; // total height
	private int segmentWidth; // about LED segmentation (7 items) for horizontal
	private int segmentHeight; // --//-- for horizontal
	private int dotWidth; // about dot for float, etc
	private BufferedImage image; // image to export from class
	private ImageIcon segmentVertical; // vertical LED segment
	private ImageIcon segmentHorizontal; // horizontal LED segment
	private ImageIcon segmentDot; // dot LED segment
	private int segmentArray[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; // array to ON or
																// OFF the
																// current
																// segment

	// constructors
	public CLEDItem() {

	}

	public CLEDItem(Image segment, Image dot, char letter, boolean comma) {

		if (setLetter(letter, comma) == 1)
			return;

		// Preparing images
		segmentHorizontal = new ImageIcon(segment);
		segmentVertical = new ImageIcon(
				toImage(rotate90SX(toBufferedImage(segment))));
		segmentDot = new ImageIcon(dot);

		// Set main measure
		segmentHeight = segment.getHeight(null);
		segmentWidth = segment.getWidth(null);
		dotWidth = dot.getWidth(null);

		height = segmentHeight * 3 + segmentWidth * 2;
		width = segmentHeight * 2 + segmentWidth + dotWidth * 2;

		// Making result background with Alpha channel
		image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

	}

	// Output Width & Height
	public int getHeight() {
		return height;
	}

	public int getWidth() {
		return width;
	}

	public BufferedImage clearImage(BufferedImage bufImg) {
		for (int x = 0; x < bufImg.getWidth(); x++) {
			for (int y = 0; y < bufImg.getHeight(); y++) {
				bufImg.setRGB(x, y, (0xFF));
			}
		}
		return bufImg;
	}

	// Drawing LED item
	public Image drawItem() {

		image = clearImage(image);
		Graphics2D graph = image.createGraphics();
		graph.setBackground(new Color(0, 0, 0, 0));

		// drawing segments on the LED area

		if (segmentArray[0] == 1)
			graph.drawImage(segmentHorizontal.getImage(), segmentHeight, 0,
					null);

		if (segmentArray[1] == 1)
			graph.drawImage(segmentVertical.getImage(), 0, segmentHeight, null);

		if (segmentArray[2] == 1)
			graph.drawImage(segmentVertical.getImage(), segmentHeight
					+ segmentWidth, segmentHeight, null);

		if (segmentArray[3] == 1)
			graph.drawImage(segmentHorizontal.getImage(), segmentHeight,
					segmentHeight + segmentWidth, null);

		if (segmentArray[4] == 1)
			graph.drawImage(segmentVertical.getImage(), 0, segmentHeight * 2
					+ segmentWidth, null);

		if (segmentArray[5] == 1)
			graph.drawImage(segmentVertical.getImage(), segmentHeight
					+ segmentWidth, segmentHeight * 2 + segmentWidth, null);

		if (segmentArray[6] == 1)
			graph.drawImage(segmentHorizontal.getImage(), segmentHeight,
					segmentHeight * 2 + segmentWidth * 2, null);

		if (segmentArray[7] == 1)
			graph.drawImage(segmentDot.getImage(), segmentHeight * 2
					+ segmentWidth, segmentHeight * 2 + segmentWidth * 2, null);

		graph.dispose();

		return toImage(image);
	}

	// This method returns a buffered image with the contents of an image
	private static BufferedImage toBufferedImage(Image im) {
		BufferedImage bi = new BufferedImage(im.getWidth(null),
				im.getHeight(null), BufferedImage.TYPE_INT_ARGB);
		Graphics bg = bi.getGraphics();
		bg.drawImage(im, 0, 0, null);
		bg.dispose();
		return bi;
	}

	// This method returns an Image object from a buffered image
	private static Image toImage(BufferedImage bufferedImage) {
		return Toolkit.getDefaultToolkit().createImage(
				bufferedImage.getSource());
	}

	// Rotate segment
	private BufferedImage rotate90SX(BufferedImage bi) {
		int width = bi.getWidth();
		int height = bi.getHeight();

		BufferedImage biFlip = new BufferedImage(height, width, bi.getType());

		for (int i = 0; i < width; i++)
			for (int j = 0; j < height; j++)
				biFlip.setRGB(j, width - 1 - i, bi.getRGB(i, j));

		return biFlip;
	}

	// Checking and setting letter to LED
	public int setLetter(char letter, boolean comma) {

		// reset array
		segmentArray[0] = 0;
		segmentArray[1] = 0;
		segmentArray[2] = 0;
		segmentArray[3] = 0;
		segmentArray[4] = 0;
		segmentArray[5] = 0;
		segmentArray[6] = 0;
		segmentArray[7] = 0;

		if (comma)
			segmentArray[7] = 1;
		if (letter == ' ')
			return 0;

		// Numeric
		// 0
		if (letter == '0') {
			segmentArray[0] = 1;
			segmentArray[1] = 1;
			segmentArray[2] = 1;
			segmentArray[4] = 1;
			segmentArray[5] = 1;
			segmentArray[6] = 1;
			return 0;
		}

		// 1
		if (letter == '1') {
			segmentArray[2] = 1;
			segmentArray[5] = 1;
			return 0;
		}

		// 2
		if (letter == '2') {
			segmentArray[0] = 1;
			segmentArray[2] = 1;
			segmentArray[3] = 1;
			segmentArray[4] = 1;
			segmentArray[6] = 1;
			return 0;
		}

		// 3
		if (letter == '3') {
			segmentArray[0] = 1;
			segmentArray[2] = 1;
			segmentArray[3] = 1;
			segmentArray[5] = 1;
			segmentArray[6] = 1;
			return 0;
		}

		// 4
		if (letter == '4') {
			segmentArray[1] = 1;
			segmentArray[2] = 1;
			segmentArray[3] = 1;
			segmentArray[5] = 1;
			return 0;
		}

		// 5
		if (letter == '5') {
			segmentArray[0] = 1;
			segmentArray[1] = 1;
			segmentArray[3] = 1;
			segmentArray[5] = 1;
			segmentArray[6] = 1;
			return 0;
		}

		// 6
		if (letter == '6') {
			segmentArray[0] = 1;
			segmentArray[1] = 1;
			segmentArray[3] = 1;
			segmentArray[4] = 1;
			segmentArray[5] = 1;
			segmentArray[6] = 1;
			return 0;
		}

		// 7
		if (letter == '7') {
			segmentArray[0] = 1;
			segmentArray[2] = 1;
			segmentArray[5] = 1;
			return 0;
		}

		// 8
		if (letter == '8') {
			segmentArray[0] = 1;
			segmentArray[1] = 1;
			segmentArray[2] = 1;
			segmentArray[3] = 1;
			segmentArray[4] = 1;
			segmentArray[5] = 1;
			segmentArray[6] = 1;
			return 0;
		}

		// 9
		if (letter == '9') {
			segmentArray[0] = 1;
			segmentArray[1] = 1;
			segmentArray[2] = 1;
			segmentArray[3] = 1;
			segmentArray[5] = 1;
			segmentArray[6] = 1;
			return 0;
		}

		// Symbols

		if (letter == '-') {
			segmentArray[3] = 1;
			return 0;
		}

		// Letters

		// A
		if (letter == 'A' || letter == 'a') {
			segmentArray[0] = 1;
			segmentArray[1] = 1;
			segmentArray[2] = 1;
			segmentArray[3] = 1;
			segmentArray[4] = 1;
			segmentArray[5] = 1;
			return 0;
		}

		// B
		if (letter == 'B' || letter == 'b') {
			segmentArray[1] = 1;
			segmentArray[3] = 1;
			segmentArray[4] = 1;
			segmentArray[5] = 1;
			segmentArray[6] = 1;
			return 0;
		}

		// C
		if (letter == 'C' || letter == 'c') {
			segmentArray[3] = 1;
			segmentArray[4] = 1;
			segmentArray[6] = 1;
			return 0;
		}

		// D
		if (letter == 'D' || letter == 'd') {
			segmentArray[2] = 1;
			segmentArray[3] = 1;
			segmentArray[4] = 1;
			segmentArray[5] = 1;
			segmentArray[6] = 1;
			return 0;
		}

		// E
		if (letter == 'E' || letter == 'e') {
			segmentArray[0] = 1;
			segmentArray[1] = 1;
			segmentArray[3] = 1;
			segmentArray[4] = 1;
			segmentArray[6] = 1;
			return 0;
		}

		// F
		if (letter == 'F' || letter == 'f') {
			segmentArray[0] = 1;
			segmentArray[1] = 1;
			segmentArray[3] = 1;
			segmentArray[4] = 1;
			return 0;
		}

		// O
		if (letter == 'O' || letter == 'o') {
			segmentArray[3] = 1;
			segmentArray[4] = 1;
			segmentArray[5] = 1;
			segmentArray[6] = 1;
			return 0;
		}

		// R
		if (letter == 'R' || letter == 'r') {
			segmentArray[3] = 1;
			segmentArray[4] = 1;
			return 0;
		}

		
		// out of support symbols
		return 1;
	}
}
